﻿#Region "Imports"
Imports Microsoft.VisualBasic
Imports System.Text
Imports System.Security.Cryptography
Imports System.IO
#End Region

Module Rexor
    '----------------------
    'Title:    Rexor
    'Function: XOR's a string in hex format with a hashed password string, then the result is reversed and XOR'ed with the hashed password again.
    'Use:      Rexor(Input.Text, Key.Text)
    'Made by Todd Davies 03.02.10
    'www.todddavies.co.uk
    '----------------------
    Public Function Rexorcrypt(ByVal Input As String, ByVal Key As String) As String
        Try
            Dim x As String
            If Right(Input, 1) = "=" Then
                Key = ToHex(Key)
                Input = Input.Remove(Input.Length - 1)
                x = xorstring(Reverse(xorstring(Input, Key)), Key)
                x = HexToString(x)
            Else
                Key = ToHex(Key)
                Input = ToHex(Input)
                x = xorstring(Reverse(xorstring(Input, Key)), Key)
                x = x + "="
            End If
            Return x
        Catch
            MsgBox("Incorrect Password")
        End Try
    End Function

    Private Function xorstring(ByVal input As String, ByVal key As String) As String
        Dim Result1 As String
        key = Hashkey(key)
        Dim a As Array = input.ToCharArray
        Dim k As Array = key.ToCharArray
        Dim q As Integer = 0
        For i = 0 To a.Length - 1
            If q = k.Length - 1 Then
                q = 0
            End If
            Result1 = Result1 + Chr((Asc(k(q))) Xor Asc(a(i)))
            q = q + 1
        Next
        Return Result1
    End Function
    'Enum HashMethod
    'MD5
    'SHA1
    'SHA384
    'End Enum

    'Function GenerateHashDigest(ByVal source As String, ByVal algorithm As HashMethod) As String
    'Dim hashAlgorithm As HashAlgorithm = Nothing
    'Select Case algorithm
    'Case HashMethod.MD5
    'hashAlgorithm = New MD5CryptoServiceProvider
    'Case HashMethod.SHA1
    'hashAlgorithm = New SHA1CryptoServiceProvider
    'Case HashMethod.SHA384
    'hashAlgorithm = New SHA384Managed
    'Case Else
    '' Error case.
    'End Select

    'Dim byteValue = Encoding.UTF8.GetBytes(source)
    'Dim hashValue = hashAlgorithm.ComputeHash(byteValue)
    'Return Convert.ToBase64String(hashValue)
    'End Function

#Region "Helper Functions"
    Private Function Hashkey(ByVal key As String)
        Dim sha1CryptoService As New SHA1CryptoServiceProvider()
        Dim byteValue = Encoding.UTF8.GetBytes(key)
        Dim hashValue = sha1CryptoService.ComputeHash(byteValue)
        Return Convert.ToBase64String(hashValue)
    End Function

    Private Function ToHex(ByVal input As String) As String
        Dim str As String = input
        Dim byteArray() As Byte
        Dim hexNumbers As System.Text.StringBuilder = New System.Text.StringBuilder
        byteArray = System.Text.ASCIIEncoding.ASCII.GetBytes(str)
        For i As Integer = 0 To byteArray.Length - 1
            hexNumbers.Append(byteArray(i).ToString("x"))
        Next
        Return hexNumbers.ToString()
    End Function
    Function HexToString(ByVal hex As String) As String
        Dim text As New System.Text.StringBuilder(hex.Length \ 2)
        For i As Integer = 0 To hex.Length - 2 Step 2
            text.Append(Chr(Convert.ToByte(hex.Substring(i, 2), 16)))
        Next
        Return text.ToString
    End Function

    Private Function Reverse(ByVal input As String)
        Dim rev() As Char = input.ToCharArray
        Array.Reverse(rev)
        Return CStr(rev)
    End Function

#End Region
End Module

